package ACWing._803区间合并;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(reader.readLine());
        List<int[]> segs = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            String[] nums = reader.readLine().split(" ");
            int s = Integer.parseInt(nums[0]);
            int e = Integer.parseInt(nums[1]);
            segs.add(new int[]{s,e});
        }

        segs.sort((a,b)->{
            if(a[0]!=b[0])
                return a[0] - b[0];
            else
                return a[1] - b[1];
        });

        int s = segs.get(0)[0], end = segs.get(0)[1], ans=1;
        for (int i = 1; i < n; i++) {
            int[] seg = segs.get(i);
            if(seg[0]> end){
                ans++;
                System.out.println(s+" "+ end);
                s = seg[0];
                end = seg[1];
            }else{
                end = Math.max(end,seg[1]);
            }
        }

        writer.write(ans+"");
        writer.flush();

        reader.close();
        writer.close();

    }
}
